<?php

namespace App\Http\Controllers\Frontend\Auth;

use App\Http\Controllers\Controller;
use App\Http\Resources\Frontend\ApiResource;
use Illuminate\Http\Request;

class ForgotPasswordController extends Controller
{

    public function __construct()
    {
        $this->middleware('captcha.verify');
    }

    public function forgot(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);

        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        switch ($response) {
            case \Password::RESET_LINK_SENT:
                $res = new ApiResource('发送重置密码邮件成功');
                break;
            case \Password::INVALID_USER:
                $res = new ApiResource('用户不存在', 0);
                break;
            default:
                $res = new ApiResource('发送重置密码邮件失败', 0);
                break;
        }
        return $res;
    }

    public function broker()
    {
        return \Password::broker('users');
    }
}
